class TransitionObserver < ActiveRecord::Observer
	
  def after_create(transition)
    return unless (transition.class == PNEngine::AutomaticTransition) ||
                  (transition.class == PNEngine::ReceiveTransition)
    
    begin
      FileUtils.mkdir_p(transition.base_path) unless File.exists?(transition.base_path)
    rescue
      raise "Error creating directory #{transition.base_path}. Creation of transition #{transition.id} aborted."
    end

    if (!transition.xslt_url && transition.incoming_places.size == 1) || transition.xslt_url
      #copy stylesheet to filesystem
      AutofireFileGenerator.initializeXSLT(transition, true)
      
      #validate stylsheet
      xsl = XML::XSLT.new
      xsl = transition.xslt_path
    end
    
    true
  end


  def before_destroy(transition)
    cleanup(transition)

    true
  end

  def cleanup(transition)
    return unless transition.class == PNEngine::AutomaticTransition
    
    begin
      FileUtils.rm_r transition.base_path, :force => :true
    rescue Exception => ex
      Merb.logger.error("Could not delete transition folder #{transition.base_path.to_s}")
    end
  end
  
end
